package HashTablePackage;

import javax.print.DocFlavor;
import java.util.*;

/**
 * @author Lzm
 * @version 1.0
 */
public class intersection_ {

  /*public static int[] intersection(int[] nums1, int[] nums2){
    int index = 0;
    Set<Integer> hashTa1 = new HashSet<>();
    Set<Integer> hashTa2 = new HashSet<>();
    for (int i = 0 ; i < nums1.length ; i ++){
      hashTa1.add(nums1[i]);
    }
    for (int i = 0 ; i < nums2.length ; i ++){
      if (hashTa1.contains(nums2[i])){
        hashTa2.add(nums2[i]);
      }
    }
    int[] result = new int[hashTa2.size()];
    for (Integer integer : hashTa2) {
      result[index++] = integer;
    }
    return result;
  }*/
  /*利用set不可重复的性质完成去重操作*/
  public int[] intersection(int[] nums1, int[] nums2) {
    Set<Integer> set1 = new HashSet<>();
    Set<Integer> set2 = new HashSet<>();
    int[] result;
    int index = 0;
    for (int i = 0 ; i < nums1.length ; i ++){
      set1.add(nums1[i]);
    }
    for (int i = 0 ; i < nums2.length ; i ++){
      if (set1.contains(nums2[i])){
        set2.add(nums2[i]);
      }
    }
    result = new int[set2.size()];
    for (Integer integer : set2) {
      result[index++] = integer;
    }
    return result;
  }
}
